package com.iemes.controller.craft;

import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.TryPriceFormMap;
import com.iemes.mapper.craft.TryPriceMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@Controller
@RequestMapping("tryprice")
public class TryPriceController extends BaseController {
	@Inject
	private TryPriceMapper tryPriceMapper;

	@RequestMapping(value = "pricing", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/technology/tryprice/pricing_list";
	}

	@ResponseBody
	@RequestMapping("pricing_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		TryPriceFormMap tryPriceFormMap = getFormMap(TryPriceFormMap.class);
		if (tryPriceFormMap.get("status") == null) {
			tryPriceFormMap.put("status", 1);
		}
		List<Map<String, Object>> tryPrice = tryPriceMapper
				.getTryPrice(tryPriceFormMap);
		try {
			LuogicPaging(pageNow, pageSize, tryPrice);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;
	}

	@RequestMapping("pricing_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		TryPriceFormMap tryPriceFormMap = tryPriceMapper.findbyFrist("id", id,
				TryPriceFormMap.class);
		model.addAttribute("tryPrice", tryPriceFormMap);
		return Common.BACKGROUND_PATH + "/technology/tryprice/pricing_edit";
	}

	// 修改工艺操作
	@ResponseBody
	@RequestMapping("pricing_editEntity")
	@SystemLog(module = "产品定价", methods = "产品定价参数-修改产品定价参数")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional
	public String editEntity() throws Exception {
		TryPriceFormMap tryPriceFormMap = getFormMap(TryPriceFormMap.class);
		try {
			tryPriceMapper.editEntity(tryPriceFormMap);
		} catch (Exception e) {
			return "false";
		}
		return "success";
	}

	// 删除工艺操作
	@ResponseBody
	@RequestMapping("pricing_deleteEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "成型工艺条件检查", methods = "成型工艺条件检查-删除工艺")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() {
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		TryPriceFormMap tryPriceFormMap = new TryPriceFormMap();
		for (int i = 0; i < str.length; i++) {
			try {
				tryPriceFormMap.put("id", str[i]);
				tryPriceFormMap.put("status", 2);
				tryPriceMapper.editEntity(tryPriceFormMap);	
			} catch (Exception e) {
				return "false";
			}
		}
		return "success";
	}

}
